
/*
* To change this template, choose Tools | Templates
* and open the template in the editor.
 */
package be.ac.ua.pats.thesisrobotica.robot.sensors;

//~--- non-JDK imports --------------------------------------------------------

import be.ac.ua.pats.thesisrobotica.robot.constants.RobotConstants;
import be.ac.ua.pats.thesisrobotica.robot.communication.Connection;
import be.ac.ua.pats.thesisrobotica.robot.main.Robot;

import lejos.nxt.comm.RConsole;

/**
 * Implementation of the ultrasonic sensor
 *
 * @author Jimmy
 */
public class UltrasonicSensor extends Sensor implements Runnable {

    lejos.nxt.UltrasonicSensor sensor;

    public UltrasonicSensor() {
        this(null);
    }

    /**
     * @param connection The connection over which notifications should be sent
     */
    public UltrasonicSensor(Connection connection) {
        super(connection);
        sensor = new lejos.nxt.UltrasonicSensor(RobotConstants.SensorPorts.ULTRASONICSENSOR);
        type = RobotConstants.Types.ULTRASONICSENSOR;
    }

    /**
     * @return The current sensor reading
     */
    public String getValue() {
        return Integer.toString(sensor.getDistance());
    }
    
    /**
     * Constantly checks wether the sensor value has changed, taking into account the threshold and time interval,
     * if this happens the method will send a notification over the connection containg the sensor-ID and the value.
     */
    public void run() {
        try {
        RConsole.println("Ultrasonic sensor running");

        int                previousValue      = 0;
        int                currentValue       = 0;
        SensorNotification sensorNotification = new SensorNotification(connection);

        if(interval == 0 && threshold == 0) {
            while (connection.isConnected()) {
                currentValue = sensor.getDistance();

                if (currentValue != previousValue) {
                    sensorNotification.setAndSend(this.id, currentValue);

                    previousValue = currentValue;
                }
            }
        } else if(interval == 0 && threshold != 0) {
            while (connection.isConnected()) {
                currentValue = sensor.getDistance();
                if (currentValue < previousValue-threshold || currentValue > previousValue+threshold) {
                                  RConsole.println("currentValue: "+currentValue);
                    sensorNotification.setAndSend(this.id, currentValue);

                    previousValue = currentValue;
                }
            }
        } else if(interval != 0 && threshold == 0) {
            while(connection.isConnected()) {
                sensorNotification.setAndSend(this.id, this.getValue());
                Thread.sleep(interval);

            }
        } else if(interval != 0 && threshold != 0) {
            while(connection.isConnected()) {
                currentValue = sensor.getDistance();
                if (currentValue < previousValue-threshold || currentValue > previousValue+threshold) {
                                  RConsole.println("currentValue: "+currentValue);
                    sensorNotification.setAndSend(this.id, currentValue);

                    previousValue = currentValue;

                    Thread.sleep(interval);
                }
            }
        }

        RConsole.println("Ultrasonic sensor stopped running");
        } catch(Exception e) {
            RConsole.println("Exception in UltrasonicSensor:run");
        }
    }
    
    public void setValue(String s) {
        throw new UnsupportedOperationException("Not supported yet.");
    }
}
